<?php

namespace App\Admin\Controllers;

use App\Http\Repositories\Redis\UserRedis;
use App\Models\PublishApply;
use App\Models\Users;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ApplyPublishController extends BaseController
{
    const   INVITE_STATUS = [
        'on'  => ['value' => 0, 'text' => '无', 'color' => 'default'],
        'off' => ['value' => 1, 'text' => '有', 'color' => 'danger'],
    ];

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑用户')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增会员')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PublishApply());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 2, function ($filter) {
                $filter->equal('id', '用户ID');
            });

        });
        $grid->model()->orderBy('created_at', 'desc');
        $grid->column('id', '会员ID')->sortable();
        $grid->column('userInfo.nickname', '会员昵称');
        $grid->column('reason', '申请原因');
        $grid->column('status', '状态')->display(function ($v) {
            if ($v == 1) {
                return '申请中';
            }
            if ($v == 2) {
                return '已通过';
            }
            if ($v == 3) {
                return '审核失败';
            }
        })->label([1 => 'primary', 2 => 'warning', 3 => 'info']);
        $grid->column('created_at', __('created_at'));

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();

        });
//
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->expandFilter();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Users::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new PublishApply());
        $form->text('user_id', '会员ID')->readonly();
        $form->display('userInfo.nickname', '会员昵称');
        $form->radio('status', '状态')->options([1 => '申请中', 2 => '已通过', 3 => '审核失败'])->when(3, function ($form) {
            $form->text('reason', '失败原因');
        });
        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        $form->saved(function ($form) {
            if (intval($form->status) == 2) {
                $model              = Users::where('id', $form->user_id)->first();
                $model->can_publish = 2;
                $model->save();
                UserRedis::getRedisInstance()->setLogout($form->user_id);
            }
        });
        return $form;
    }

}
